package com.saltedfish.tool.dialog;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.saltedfish.tool.R;
import com.saltedfish.tool.model.UpdateVersion;
import com.saltedfish.tool.model.UpdateVersionInfo;
import com.saltedfish.tool.util.DownLoadUtils;

import butterknife.BindView;
import cn.droidlover.xdroidmvp.popwindow.AlertButtonDialog;
import cn.droidlover.xdroidmvp.popwindow.DialogHelper;

public class UIUpdateVersionDialog extends DialogHelper {

    @BindView(R.id.tvVersion)
    TextView tvVersion;

    @BindView(R.id.tvContent)
    TextView tvContent;

    @BindView(R.id.icon_close)
    ImageView icon_close;

    @BindView(R.id.tvUpdateVerison)
    TextView tvUpdateVerison;

    public UIUpdateVersionDialog(Activity context) {
        super(context, R.style.AlertDialogStyle);
        this.mContext = context;
        setFullWidthScreen();
        setGravity(Gravity.CENTER);
//        setAnimSet(R.style.popBottomPopAnim);

    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_ui_update_version;
    }

    private UpdateVersion updateVersions;

    public void updateVersion(UpdateVersion updateVersion) {
        this.updateVersions = updateVersion;
        String version = "V" + updateVersion.versionName;
        tvVersion.setText(version);
        tvContent.setText(updateVersion.updateContent);
    }

    @Override
    protected void initView() {
        icon_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });
        tvUpdateVerison.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                checkForUpdateSuccess();
                dismiss();
            }
        });
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void dialogDismiss() {

    }


    public void checkForUpdateSuccess() {
        if (updateVersions != null) {
            if (!TextUtils.isEmpty(updateVersions.packageLinkAddress)) {
                //0:表示需要更新 1：表示最新 2 表示客户端大于服务器版本（视为不需要跟新）
                if (Build.VERSION.SDK_INT >= 23) {
                    if (PackageManager.PERMISSION_GRANTED == ContextCompat.checkSelfPermission(mContext,
                            Manifest.permission.READ_EXTERNAL_STORAGE) ||
                            PackageManager.PERMISSION_GRANTED == ContextCompat.checkSelfPermission(mContext,
                                    Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                        DownLoadUtils.download(
                                mContext,
                                updateVersions.packageLinkAddress,
                                "flag");
                    } else {
                        // ToastUtils.showMessage(context, "存储权限被禁止，请前往系统设置->应用管理->咸鱼Flag下权限管理开启存储权限");
                        return;
                    }
                } else {
                    DownLoadUtils.download(
                            mContext,
                            updateVersions.packageLinkAddress,
                            "flag");
                }
            }

        }

    }

}
